package com.wn.service.impl;

import com.baidu.aip.ocr.AipOcr;
import com.wn.dao.IdCardDao;
import com.wn.service.SampleService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
@Service("service")
@Transactional(rollbackFor = Exception.class)
public class SampleServiceImpl implements SampleService {

    @Autowired
    private IdCardDao idCardDao;
    //设置APPID/AK/SK
    public static final String APP_ID = "22893690";
    public static final String API_KEY = "37OuGMm2yOygF3TR7woqZ0sX";
    public static final String SECRET_KEY = "wV52eFIe9ToN36TyzogO6v8VBjFVBRWT";

    public  String idcard(String file) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String path = file;
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(5));
        int i = idCardDao.addOne(res.toString(2));
        if(i>0){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    @Override
    public String upIdImg(String idMsg) {
        int i = idCardDao.addOne(idMsg);
        if(i>0){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
}
